<template>
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="rules" @submit.native.prevent="handleLogin">
        <el-form-item label="账号" prop="username">
          <el-input v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        </el-form-item>
        <router-link to="/register" class="register-link">没有账号？马上注册！</router-link>
      </el-form>
    </div>
  </template>
  
  <script>
  export default {
    name: 'PetLogin',
    data() {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 20, message: '账号长度必须在3到20个字符之间', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '密码长度必须在6到20个字符之间', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loginSuccess();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      loginSuccess() {
        // 设置登录成功标志
        localStorage.setItem('isLoggedIn', 'true');
        this.$emit('update:isLoggedIn', true);
        this.$router.push('/create-profile'); // 跳转到宠物档案页面
      }
    }
  };
  </script>
  
  <style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center; /* 居中所有子元素 */
  width: 300px;
  margin: 50px auto;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.el-form-item {
  width: 100%; /* 确保表单项宽度与容器一致 */
}

.register-link {
  display: block;
  margin-top: 10px;
  color: #42b983;
}
</style>
